<template>
  <div class="goods">
      <!-- 方法一实现跳转 使用window.location.href的形式 ,编程式导航 -->
    <div class="goodsList" 
     v-for="item in goodsList" :key="item.id"
     @click="getDetil(item.id)"
    >    
      <img :src="item.img_url"  />
      <h3 class="title">{{item.title}}</h3>
      <div class="info">
        <p class="price">
          <span class="now">${{item.sell_price}}</span>
          <span class="old">${{item.market_price}}</span>
        </p>
        <p class="sell">
          <span>热卖中</span>
          <span>剩余:{{item.stock_quantity}}</span>
        </p>
      </div>
    </div>
    <!-- 方法二实现跳转  使用标签,标签跳转-->
        <!-- <router-link class="goodsList" 
     v-for="item in goodsList" :key="item.id"
    :to="'/home/goodsInfo/'+item.id" tag="div"
    >    
      <img :src="item.img_url"  />
      <h3 class="title">{{item.title}}</h3>
      <div class="info">
        <p class="price">
          <span class="now">${{item.sell_price}}</span>
          <span class="old">${{item.market_price}}</span>
        </p>
        <p class="sell">
          <span>热卖中</span>
          <span>剩余:{{item.stock_quantity}}</span>
        </p>
        </div>
      </router-link> -->
    
    <mt-button type="danger" size="large" plain @click="getMore">加载更多</mt-button>
  </div>
</template>
 
<script>
export default {
  name: "",
  data() {
    return {
        goodsList:[],
        pageIndex:1
    }
  },
  mounted(){
      this.getGoodsList();
  },
  methods:{
      getGoodsList(){
          //获取商品列表
          this.$http.get("api/getgoods?pageIndex="+this.pageIndex)
           .then(res=>{
               if(res.body.status===0){
                //    console.log(res.body.message);
                   this.goodsList=this.goodsList.concat(res.body.message)

               }
           })
      },
      getMore(){//加载更多
         this.pageIndex ++;
         this.getGoodsList();
     },
    //方法一配套
     getDetil(id){
         //使用js的的形式进行路由导航
        //  区分this.$route和this.$router
        //this.$route是路由参数对象,所有的路由中的参数
        //this.$router是一个路由导航对象,用它可以方便的使用js
        //实现路由的前进,后退,跳转到新的URL地址
        //1.最简单的
        // this.$router.push("/home/goodsInfo/"+id)
        //2.传递对象
        // this.$router.push({path:"/home/goodsInfo/"+id})
        //3.传递命名路由
        this.$router.push({name:"goodsInfo",params:{id}})
        //path不能和params一起用

     }
  }
};
</script>
 
<style scoped lang = "scss">
.goods{
    display: flex;
    flex-wrap: wrap;
    padding: 7px;
    justify-content: space-between;
    .goodsList{
       width: 49%;
       border:1px solid #ccc;
       box-shadow: 0 0 8px #ccc;
       margin: 4px 0;
       padding: 2px;
       display: flex;
       flex-direction: column;
       justify-content: space-between;
       min-height: 293px;
       img{
           width: 100%;
       }
       .title{
           font-size: 14px;
       }
       .info{
           background-color: #eee;
           padding: 5px;
           p{
               margin: 0;
           }
           .price{
               .now{
                   color: red;
                   font-weight: bold;
                   font-size: 16px;
               }
               .old{
                   text-decoration: line-through;
                   font-size: 12px;
                   margin-left: 10px;

               }
           }
           .sell{
               display: flex;
               justify-content: space-between;
               font-size: 12px;
           }
       }
    }
}

</style>